package com.zx.viewlearn.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by zhoux on 2018/1/8.
 * 注释：自定义view 画一个钟
 */

public class MyView13Clock extends View {
    private String TAG = "--------MyView13Clock";
    float radius = 280F;
    Paint mPaint;

    public MyView13Clock(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.i(TAG, "MyView13Clock()");
        init();
    }

    public MyView13Clock(Context context) {
        super(context);
        Log.i(TAG, "MyView13Clock()");
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.BLUE);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        Log.i(TAG, "onFinishInflate()");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.i(TAG, "onMeasure()");
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.i(TAG, "onSizeChanged()");

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.i(TAG, "onLayout()");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i(TAG, "onDraw()");

        canvas.drawCircle(radius, radius, radius, mPaint);//画圆

        // TODO: 2018/1/15 画指针 1
//        canvas.save();
//        canvas.translate(radius, radius);
//        mPaint.setStrokeWidth(5);
//        canvas.drawLine(0, 0, (float) (radius * 0.5), (float) (radius * 0.5), mPaint);
//        mPaint.setStrokeWidth(16);
//        canvas.drawLine(0, 0, (float) (radius * 0.3), -(float) (radius * 0.3), mPaint);
//        canvas.restore();

        //todo 画刻度 和 时间文字
        canvas.save();
        mPaint.setStrokeWidth(3);

        for (int i = 0; i < 60; i++) {
            canvas.rotate(6, radius, radius);

            int stopY = 60;
            float textSize = 30;
            String text = i / 5 == 0 ? "12" : i / 5 + "";

            if (i % 15 == 0) {//1/4小时
                stopY = 60;
                textSize = 30;
            } else if (i % 5 == 0) {//5分钟
                stopY = 30;
                textSize = 20;
            } else {//1分钟
                stopY = 10;
                textSize = 0;
            }
            canvas.drawLine(radius, 0, radius, stopY, mPaint);
            mPaint.setTextSize(textSize);
            canvas.drawText(text, radius - mPaint.measureText(text) / 2, stopY + textSize, mPaint);


        }
        canvas.restore();
        drawHand(canvas);
    }


    float degrees = 6;
    int second = 0;
    float min = 0;
    float hour = 0;

    private void drawHand(Canvas canvas) {
        // TODO: 2018/1/15 画指针 2
        //秒针
        canvas.save();
        mPaint.setStrokeWidth(2);
        canvas.translate(radius, radius);
        canvas.rotate(degrees * second);
        canvas.drawLine(0, 0, 0, -(float) (radius * 0.7), mPaint);
        canvas.restore();

        //分针
        canvas.save();
        mPaint.setStrokeWidth(5);
        canvas.translate(radius, radius);
        canvas.rotate(degrees * min);
        canvas.drawLine(0, 0, 0, -(float) (radius * 0.5), mPaint);
        canvas.restore();

        //时针
        canvas.save();
        mPaint.setStrokeWidth(10);
        canvas.translate(radius, radius);
        canvas.rotate(degrees * hour);
        canvas.drawLine(0, 0, 0, -(float) (radius * 0.25), mPaint);
        canvas.restore();


        second = second + 1;
        min = second / 60F;
        hour = min / 60F;
        postInvalidateDelayed(10);
    }

//    // TODO: 2018/1/15 动起来
//    private void move() {
//        invalidate();
//    }

}
